﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Visifire.Charts;
using Visifire.Commons;

namespace WPFCouse
{
    /// <summary>
    /// VisifireCharts.xaml 的交互逻辑
    /// </summary>
    public partial class VisifireCharts : Window
    {
        DataSeries dataSeries;

        public VisifireCharts()
        {
            InitializeComponent();
            makeChart();
            //makeChart1();
            //gridChart.Children.Add(CreateChart());
        }

        List<DataPoint> dataPoints = new List<DataPoint>(); 

        private void makeChart()
        {
            //准备数据源
            var products = new List<Tuple<int, double>>{
                Tuple.Create(0, 0.018),
                Tuple.Create(24, 0.035),
                Tuple.Create(136, 0.053),
                Tuple.Create(148, 0.046),
                Tuple.Create(260, 0.023),
                };

            //创建图表
            //Chart chart = new Chart();
            SolidColorBrush myBrush1 = new SolidColorBrush(Colors.Red);
            //创建数据服务对象
            dataSeries = new DataSeries
            {
                RenderAs = RenderAs.Spline,
                LabelEnabled = false,
                YValueFormatString = "0.000",
                LegendText = "Inspected",
                Color = myBrush1,
                //Legend = "销量曲线图",
                SelectionEnabled = true,
                StartAngle = 10,
                
            };

            //创建数据服务对象
            DataSeries testDS = new DataSeries
            {
                RenderAs = RenderAs.Spline,
                LabelEnabled = false,
                YValueFormatString = "0.000",
                LegendText = "Target",
                //Legend = "销量曲线图",
                SelectionEnabled = true,
                StartAngle = 10,

            };

            //创建“数据点”对象，将数据源中指定的属性数据添加至数据服务对象
            //注意X坐标数据类型为：string，Y坐标数据为：double
            products.ForEach(p => {
                var datapoint = new DataPoint
                {
                    XValue = p.Item1,
                    YValue = p.Item2,
                    MarkerSize = 20
                };

                //var datapoint1 = new DataPoint
                //{
                //    XValue = p.Item1,
                //    YValue = 5,
                //    MarkerSize = 20
                //};

                dataPoints.Add(datapoint);
                //将数据源中每一个数据点，“挂载”到数据服务对象下的数据点对象
                dataSeries.DataPoints.Add(datapoint);
               
            });

            testDS.DataPoints.Add(new DataPoint
            {
                XValue = 0,
                YValue = 5,
                MarkerSize = 20
            });
            testDS.DataPoints.Add(new DataPoint
            {
                XValue = 360,
                YValue = 5,
                MarkerSize = 20
            });
            //Series.DataContext = dataSeries;

            //将数据服务对象添加到图表对象
            //即在图表中“画”一个你需要的图像，若需要在一个图表中“画”更多图像（例如N条曲线），重复上面步骤后添加到同一个图表对象就可以了）
            viewChart.Series.Add(dataSeries);
            viewChart.Series.Add(testDS);

            //最后将这个图表元素添加到前端布局容器中即可
            //gridChart.Children.Add(chart);
        }

        

        private void makeChart1()
        {
            //准备数据源
            //var products = new List<Tuple<int, double>>{
            //    Tuple.Create(12, 0.018),
            //    Tuple.Create(24, 0.035),
            //    Tuple.Create(36, 0.053),
            //    Tuple.Create(48, 0.046),
            //    Tuple.Create(60, 0.023),
            //    };

            //创建图表
            //Chart chart = new Chart();

            //创建数据服务对象
            dataSeries = new DataSeries
            {
                RenderAs = RenderAs.Spline,
                LabelEnabled = true,
                YValueFormatString = "0.000",
                LegendText = "最小值",
                Legend = "销量曲线图",
                SelectionEnabled = true,
                StartAngle = 10,
            };

            //创建“数据点”对象，将数据源中指定的属性数据添加至数据服务对象
            //注意X坐标数据类型为：string，Y坐标数据为：double
            //products.ForEach(p => {
            //    var datapoint = new DataPoint
            //    {
            //        AxisXLabel = p.Item1.ToString(),
            //        YValue = p.Item2,
            //    };

            //    //将数据源中每一个数据点，“挂载”到数据服务对象下的数据点对象
            //    dataSeries.DataPoints.Add(datapoint);
            //});

            dataSeries.DataPoints.Add(new DataPoint
            {
                AxisXLabel = "15",
                YValue = 0.018,
            });
            //Series1.DataContext=dataSeries;
            //将数据服务对象添加到图表对象
            //即在图表中“画”一个你需要的图像，若需要在一个图表中“画”更多图像（例如N条曲线），重复上面步骤后添加到同一个图表对象就可以了）
            viewChart.Series.Add(dataSeries);

            //最后将这个图表元素添加到前端布局容器中即可
            //gridChart.Children.Add(chart);
        }

        public Chart CreateChart()
        {
            Chart chart = new Chart();
            chart.ToolBarEnabled = true;//工具箱是否可用：具有保存、打印图片功能
            chart.AnimatedUpdate = true;//是否动画更新
            chart.AnimationEnabled = true;//是否动画可用
            chart.ScrollingEnabled = false;//是否启用或禁止滚动
            chart.IndicatorEnabled = true;//指示线是否可用
            chart.View3D = false;//是否3D效果显示
            chart.Theme = "Theme1";//图表主题设置

            //定义图例（可不定义，图表本身自带图例）：图例位置 + 字体样式
            Legend legend = new Legend();
            legend.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;//水平方向位置
            legend.VerticalAlignment = System.Windows.VerticalAlignment.Center;//竖直方向位置
            legend.FontSize = 12;//字体大小
            legend.FontFamily = new FontFamily("宋体,Times New Roman");//字体样式
            chart.Legends.Add(legend);//图表添加图例

            //定义图表标题：标题文本、字体样式设置
            Title title = new Title();
            title.FontFamily = new FontFamily("宋体,Times New Roman");
            title.Text = "图表名";
            title.FontSize = 20;
            chart.Titles.Add(title);//图表添加标题

            //定义x轴
            Axis xAxis = new Axis();
            xAxis.IntervalType = IntervalTypes.Auto;
            xAxis.Interval = 1;//x轴的间隔
                               //定义x轴标签：字体样式设置
            AxisLabels xLabels = new AxisLabels();
            xLabels.FontFamily = new FontFamily("宋体,Times New Roman");
            xLabels.FontSize = 12;
            xAxis.AxisLabels = xLabels;

            //定义y轴
            Axis yAxis0 = new Axis();
            AxisLabels yLabels0 = new AxisLabels();
            yLabels0.FontFamily = new FontFamily("宋体,Times New Roman");
            yLabels0.FontSize = 14;
            yAxis0.Suffix = "kg";//y轴单位设置
            yAxis0.AxisMinimum = 0;//y轴最小值设置
            yAxis0.AxisMaximum = 20;//最大值设置
            yAxis0.AxisLabels = yLabels0;

            //双轴定义方法：定义第二条y轴
            Axis yAxis1 = new Axis();
            yAxis1.AxisType = AxisTypes.Secondary;
            AxisLabels yLabels1 = new AxisLabels();
            yLabels1.FontFamily = new FontFamily("宋体,Times New Roman");
            yLabels1.FontSize = 14;
            yAxis1.Suffix = "m";
            yAxis1.AxisMinimum = 0;
            yAxis1.AxisMaximum = 20;
            yAxis1.AxisLabels = yLabels1;

            chart.AxesX.Add(xAxis);//图表添加x轴
            chart.AxesY.Add(yAxis0);//图表添加y轴
            chart.AxesY.Add(yAxis1);

            //定义第一条数据线
            DataSeries series0 = new DataSeries();
            series0.RenderAs = RenderAs.Line;//图表类型：曲线图、饼图、柱状图
            series0.LabelEnabled = false;//标签是否可见
            series0.LegendText = "图例1";//图例文本内容
            series0.ShowInLegend = true;//图例是否显示
            series0.MarkerType = MarkerTypes.Cross;//曲线上数据点的形状
            series0.MarkerSize = 10;//数据点的形状的大小
            series0.LineThickness = 1;//数据线的粗细

            //数据线添加数据点
            Random r = new Random();
            for (int j = 0; j < 10; j++)
            {
                DataPoint dataPoint = new DataPoint();
                dataPoint.AxisXLabel = "X" + j;//横坐标
                dataPoint.YValue = r.Next(0, 10);//纵坐标
                                                 //数据点的标签字体样式设置
                dataPoint.LabelFontSize = 15;
                dataPoint.LabelFontFamily = new FontFamily("宋体,Times New Roman");

                //if (j == 5)
                //{
                //    dataPoint.Selected = true;
                //}
                //dataPoint.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);

                //dataPoint_MouseLeftButtonDown(dataPoint, null);

                series0.DataPoints.Add(dataPoint);//数据线添加数据点
            }

            //定义第二条数据线
            DataSeries series1 = new DataSeries();
            series1.RenderAs = RenderAs.Line;
            series1.LegendText = "图例2";
            series1.MarkerSize = 10;
            series1.LineThickness = 1;
            series1.AxisYType = AxisTypes.Secondary;//数据线采用第二条y轴

            for (int j = 0; j < 10; j++)
            {
                DataPoint dataPoint = new DataPoint();
                dataPoint.AxisXLabel = "X" + 5;
                dataPoint.YValue = r.Next(10, 20);
                dataPoint.LabelFontSize = 15;
                dataPoint.LabelFontFamily = new FontFamily("宋体,Times New Roman");
                series1.DataPoints.Add(dataPoint);
                if (j == 5)
                {
                    dataPoint.Selected = true;
                }
            }

            //图表添加数据线
            chart.Series.Add(series0);
            chart.Series.Add(series1);
            return chart;
        }

        private void dataPoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataPoint dataPoint = sender as DataPoint;

            dataPoint.Selected = true;
        }

        private void test_Click(object sender, RoutedEventArgs e)
        {
           var chart= dataPoints.FirstOrDefault(p=>p.XValue.Equals(24)&&p.YValue== 0.035);
            chart.Selected = true;
            //dataPoints[3].Selected = true;
        }
    }
}
